package cn.chencq.proxy.timer.handler.custom;

import java.net.Proxy;
import java.util.HashSet;
import java.util.Set;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import cn.chencq.proxy.timer.handler.GeneralProxyHandler;

/**
 * @website http://www.goubanjia.com
 * @author chencq
 * @desc gbj 每次刷新会返回不同数据
 * 
 */
public class ProxyHandlerByGbj extends GeneralProxyHandler {

	@Override
	public String getHandler() {

		return "GBJ";
	}

	@Override
	public int parseResp(String rid,Document doc) {
		Set<String> proxySet = new HashSet<String>();
		Elements tbody = doc.select("tbody");
		for (Element tb : tbody) {
			Elements trs = tb.getElementsByTag("tr");
			StringBuilder ip = null;
			for (Element tr : trs) {
				Elements tds = tr.getElementsByTag("td");
				Elements children = tds.get(0).children();
				ip = new StringBuilder();
				for (int i = 0; i < children.size(); i++) {
					Element ch = children.get(i);
					String outhtml = ch.outerHtml();
					if (outhtml.replaceAll("[\\s\\n]", "").contains("display:none;")) {
						continue;
					} else {
						ip.append(ch.html());
					}
				}
				
				String port = tds.get(1).html();
				proxySet.add(ip.append(":").append(port).toString());
			}
		}
		super.putToCache(rid,proxySet);
		return proxySet.size();
	}

	@Override
	public int crawler(String rid,String url,Proxy proxy) {
		int ret = 0;
		try {
			Document doc = requestDoc(rid,url,proxy);
			if (doc != null) {
				ret = parseResp(rid,doc);
			} else {
				ret = 0;
			}
		} catch (Exception e) {
			ret = 0;
		}
		return ret;
	}

}
